package ren.nearby.home;

import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ogaclejapan.smarttablayout.SmartTabLayout;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItemAdapter;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItems;
import com.orhanobut.logger.Logger;


import javax.inject.Inject;

import hugo.weaving.DebugLog;
import ren.nearby.bean.beans.LoginBean;
import ren.nearby.home.mvp.FragmentContract;
import ren.nearby.http.base.BaseActivity;


public class ANearbyMain extends BaseActivity implements ViewPager.OnPageChangeListener {


    //    @BindView(R2.id.home_tabLayout)
    public SmartTabLayout home_tabLayout;
    //    @BindView(R2.id.home_viewPager)
    public NearbyViewPager home_viewPager;

    @Override
    public int getContentView() {
        return R.layout.home_activity_anearby_main;
    }



    @Inject
    LoginBean loginBean;

    @Override
    public void initView() {
        super.initView();

        home_tabLayout = findViewById(R.id.home_tabLayout);
        home_viewPager = findViewById(R.id.home_viewPager);
        FragmentPagerItems pages = FragmentPagerItems.with(this)
                .add(R.string.home_nearby_fragment_home, NearbyFragmentHome.class)
                .add(R.string.home_nearby_fragment_message, NearbyFragmentMessage.class)
                .add(R.string.home_nearby_fragment_me, NearbyFragmentMe.class)
                .create();
        FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
                getSupportFragmentManager(),
                pages);
        Logger.e(home_viewPager == null ? "1" : "0");
        home_viewPager.setOffscreenPageLimit(pages.size());
        home_viewPager.setAdapter(adapter);
        home_viewPager.setScanScroll(true);
        home_viewPager.addOnPageChangeListener(this);
        setup(home_tabLayout);
        home_tabLayout.setViewPager(home_viewPager);


        Logger.e(loginBean == null ?
                "value = 1" :
                " value = 0" + loginBean.getUsername() + " - " + loginBean.isIps());
        //测试Carch库
//    testCrach();
    }

    public void testCrach() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                throw new NullPointerException("00");
            }
        }).start();
    }

    //    @DebugLog


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @DebugLog
    //添加tab图标
    public void setup(SmartTabLayout layout) {
        layout.setCustomTabView(new SmartTabLayout.TabProvider() {
            @Override
            public View createTabView(ViewGroup container, int position, PagerAdapter adapter) {
                View view = LayoutInflater.from(ANearbyMain.this).inflate(R.layout.main_menu_layout, container, false);
                ImageView icon = (ImageView) view.findViewById(R.id.icon);
                TextView name = (TextView) view.findViewById(R.id.name);
                name.setText(adapter.getPageTitle(position));
                switch (position) {
                    case 0:
                        icon.setImageDrawable(getResources().getDrawable(R.drawable.nearby_home_menu_selector));
                        break;
                    case 1:
                        icon.setImageDrawable(getResources().getDrawable(R.drawable.nearby_message_menu_selector));
                        break;
                    case 2:
                        icon.setImageDrawable(getResources().getDrawable(R.drawable.nearby_me_menu_selector));
                        break;
                    default:
                        throw new IllegalStateException("Invalid position: " + position);
                }
                return view;
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Logger.e(" main Home 组件 " + "requestCode = " + requestCode + " - resultCode = " + resultCode);
    }
}
